<template>
<!--面包屑-->
<div style="margin-bottom: 30px">
  <el-breadcrumb separator="/">
    <el-breadcrumb-item :to="{ path: '/Main' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>
      <router-link to="/UserInfo/UserQuery">用户列表</router-link>
    </el-breadcrumb-item>
    <el-breadcrumb-item>用户信息列表</el-breadcrumb-item>
  </el-breadcrumb>
</div>

  <!--查询条件-->
  <div style="margin-bottom: 30px;">
    手机号：<el-input v-model="form.phone" placeholder="请输入手机号" style="width: 240px;"></el-input>&nbsp;&nbsp;
    状态：<el-select v-model="form.login_flag" placeholder="请选择状态" style="width: 240px;">
      <el-option label="全部" :value="-1" />
      <el-option label="正常" :value="1" />
      <el-option label="禁用" :value="0" />
    </el-select>&nbsp;&nbsp;
    <br/>
    <br/>
    <!-- 名字:<el-input v-model="form.name" placeholder="请输入名字" style="width: 240px;"></el-input> -->
    名字:<el-select v-model="form.name" placeholder="请选择名字" style="width: 240px;"  >
      <el-option v-for="item in UserSelectData" :key="item.id" :label="item.name" :value="item.name"/>
    </el-select>
    &nbsp;&nbsp;
    工号：<el-input v-model="form.no" placeholder="请输入工号" style="width: 240px;"></el-input>&nbsp;&nbsp;
    
    <el-button type="primary" @click="getUserInfoList">查询</el-button>
    <el-button @click="reset">重置</el-button>
    <br/>
    <br/>
    <el-button type="primary" @click="router.push('AddEmployeeUser')">新增</el-button>&nbsp;&nbsp;
  </div>
  
  <!--物资列表-->
<div>
  <el-table :data="UserInfoData" border style="width: 100%" @selection-change="handleSelectionChange"  :default-sort="{ prop: 'create_date', order: 'descending' }" >
      <el-table-column type="selection" />
      <el-table-column prop="id" label="编号" align="center" />
      <el-table-column prop="login_name" label="账号" align="center" width="90px" />
      <el-table-column prop="no" label="工号" align="center" />
      <el-table-column prop="name" label="名称" align="center" />
      <el-table-column prop="email" label="邮箱" align="center" width="168px" />
      <el-table-column prop="phone" label="手机" align="center" width="110px" />
      <el-table-column prop="user_type" label="员工类别" align="center" width="81px" >
        <template #default="scope">
          <span v-if="scope.row.user_type == 0">员工</span>
          <span v-else-if="scope.row.user_type == 1">组长</span>
          <span v-else-if="scope.row.user_type == 2">主管</span>
          <span v-else-if="scope.row.user_type == 3">总经理</span>
          <span v-else-if="scope.row.user_type == 110">董事长</span>
        </template>
      </el-table-column>
      <el-table-column prop="sex" label="性别" align="center" >
        <template v-slot="scope">
          <span v-if="scope.row.sex == 1">男</span>
          <span v-else-if="scope.row.sex == 0">女</span>
        </template>
      </el-table-column>
      <el-table-column prop="mgr_type" label="管理员类别" align="center" width="95px" >
        <template v-slot="scope">
          <span v-if="scope.row.mgr_type == 0">普通用户</span>
          <span v-else-if="scope.row.mgr_type == 1">管理员</span>
        </template>
      </el-table-column>
      <el-table-column prop="avatar" label="头像" align="center" />
      <el-table-column prop="login_ip" label="ip地址" align="center" width="125px" />
      <el-table-column prop="login_date" label="最后登录时间" align="center" width="211px">
        <template v-slot="scope">
          {{ scope.row.login_date }}
          <!-- {{ scope.row.login_date.replace("T"," ") }} -->
          <!-- {{ scope.row.login_date.substr(0,10) }} -->
        </template>
      </el-table-column>
      <el-table-column prop="login_flag" label="可否登录" align="center" width="81px" >
        <template v-slot="scope">
          <span v-if="scope.row.login_flag == 1">启用</span>
          <span v-else-if="scope.row.login_flag == 0">禁用</span>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="create_by" label="创建人" align="center" /> -->
      <el-table-column prop="create_date" label="创建时间" align="center" width="162px" >
        <template v-slot="scope">
          <!-- {{ scope.row.create_date }} -->
          {{ scope.row.create_date.replace("T"," ").substr(0,19) }}
          <!-- {{ scope.row.create_date.substr(0,10) }} -->
        </template>
      </el-table-column>
      <!-- <el-table-column prop="update_by" label="修改人" align="center" /> -->
      <el-table-column prop="update_date" label="修改时间" align="center" width="211px" >
        <template v-slot="scope">
          {{ scope.row.update_date }}
          <!-- {{ scope.row.update_date.replace("T"," ").substr(0,19) }} -->
          <!-- {{ scope.row.update_date.substr(0,10) }} -->
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="160px" >
        <template #default="scope">
          <el-button type="primary" @click="Editor(scope.row.id)">编辑</el-button>
          <el-button type="danger" @click="deleteUser(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
</div>
  
  <!--分页-->
  <div>
    <table>
    <thead></thead>
    <tbody>
      <tr>
        <td>
          <el-pagination v-model:current-page="form.pageIndex" v-model:page-size="form.pageSize" :page-sizes="[3, 5, 7, 9]" :background="true" layout="total,slot, sizes" :total="form.totalCount" prev-text="上一页" next-text="下一页" @size-change="handleSizeChange" @current-change="handleCurrentChange">
            <span>
              ，每页{{ form.pageSize }}条，当前第:{{ form.pageIndex }}/{{ form.totalPage }}页
            </span>
          </el-pagination>
        </td>
  
        <td>
          <el-pagination v-model:current-page="form.pageIndex" v-model:page-size="form.pageSize" :page-sizes="[100, 200, 300, 400]" :background="true" layout="slot, prev, pager, next" :total="form.totalCount" prev-text="上一页" next-text="下一页" @size-change="handleSizeChange" @current-change="handleCurrentChange">
            <el-button type="primary" class="btn-prev" @click="handleCurrentChange(1)" :disabled="form.pageIndex==1">首页</el-button>
          </el-pagination>
        </td>
  
        <td>
          <el-pagination v-model:current-page="form.pageIndex" v-model:page-size="form.pageSize" :page-sizes="[100, 200, 300, 400]" :background="true" layout="slot, jumper" :total="form.totalCount" prev-text="上一页" next-text="下一页" @size-change="handleSizeChange" @current-change="handleCurrentChange">
            <el-button type="primary" class="btn-prev" @click="handleCurrentChange(form.totalPage)" :disabled="form.pageIndex==form.totalPage">尾页</el-button>
          </el-pagination>
        </td>
      </tr>
    </tbody>
  </table>
  </div>
  </template>
  
  <script setup lang="ts">
  import { reactive, ref, onMounted, computed } from 'vue'
  import axios from 'axios';
  import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
  import { ElMessage, ElMessageBox } from 'element-plus'
  
  import {useRoute,useRouter} from 'vue-router'
  
  const route = useRoute();
  const router = useRouter();
  const dialogFormVisible:any = ref(false)
const formLabelWidth = '140px'
const formSize = ref<ComponentSize>('default')

  
  onMounted(() => {
    //用户列表
    getUserInfoList()

    getUserSelect()
  })
  
  const form: any = reactive({
    phone: '',
    login_flag: -1,
    name: '',
    no:'',
    pageIndex: 1,
    pageSize: 5,
    totalPage: 0,
    totalCount: 0,
  })

  //角色名称列表
  const UserSelectData:any=ref([])
const getUserSelect=()=>{
  axios.get('http://1.117.93.87:8000/rbacdu/api/User/UserSelectShow').then(res=>{
    //console.log(res);
    UserSelectData.value = res.data.data
  }).catch(err=>{
    console.log(err);
    
  })    
}

  //存储多选中的物资id
const multipleSelection:any = ref([]);
  const handleSelectionChange = (val: any) => {
    // console.log(val);
    multipleSelection.value = val;
  }
  
  
  //分页
  const handleSizeChange = (val: number) => {
    form.pageSize = val;
    getUserInfoList()
  }
  const handleCurrentChange = (val: number) => {
    form.pageIndex = val;
    getUserInfoList()
  }
  
  //用户列表
  const UserInfoData:any=ref([])
  const getUserInfoList = () => {
  
    axios.get('http://1.117.93.87:8000/rbacdu/api/User/userShow', {
      params: {
        phone: form.phone,
        login_flag: form.login_flag,
        name: form.name,
        no: form.no,
        pageIndex: form.pageIndex,
        pageSize: form.pageSize,
      }
    }).then(res=>{
      console.log(res);
      UserInfoData.value = res.data.data.list;
      form.totalPage = res.data.data.pageCount;
      form.totalCount = res.data.data.totalCount;
    }).catch(err=>{
      console.log(err);
      
    })    
  }
  
  //重置
  const reset = () => {
    form.phone = '',
    form.login_flag = -1,
    form.name = '',
    form.no = '',
    form.pageIndex = 1,
    form.pageSize = 5,
    getUserInfoList()
  }


  //编辑
  const Editor = (id:number) => {
    router.push({
      path: '/UserInfo/UpdateUserQuery',
      query: {
        id: id
      }
    })
  }

  //删除
  const deleteUser = (id:number) => {
    ElMessageBox.confirm(
      '确认删除该用户信息吗?',
      '确认删除',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }
    )
      .then(() => {
        axios.post('http://1.117.93.87:8000/rbacxie/api/User/userDelete',[id]).then(res => {
          console.log(res);
          if (res.data.code >= 0) {
            ElMessage.success("删除成功!")
            //刷新页面
            getUserInfoList()
          } else {
            ElMessage.error("删除失败!")
            return
          }
        }).catch(err => {
          console.log(err);
  
        })
      })
      .catch(() => {
        ElMessage({
          type: 'info',
          message: '取消操作',
        })
      })
  }

  </script>
  
  <style scoped>
  
  </style>